package com.dataextend.service;


import com.alibaba.fastjson.JSONObject;
import com.dataextend.common.utils.CommonUtil;
import com.dataextend.entity.UserGroup;
import com.dataextend.entity.UserGroupChange;
import com.dataextend.mapper.cluster.UserGroupMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *   用户画像—— 用户人群列表
 */
@Service
public class UserGroupService {
    @Autowired
    private UserGroupMapper userGroupMapper;

    /**
     *  用户人群列表 分页展示
     */
    public JSONObject listUsergroup(Integer pageNo, Integer pageSize){
        int count = userGroupMapper.countUserGroup();
        Integer paramPageno = (pageNo - 1) * pageSize;
        List<JSONObject> list = userGroupMapper.listUsergroup(paramPageno, pageSize);
        JSONObject returnResult = new JSONObject(true);
        returnResult.put("list", list);
        returnResult.put("total", count);
        return returnResult;
    }


    /**
     *  新增用户人群
     */
    public JSONObject addUsergroup(UserGroup userGroup){
        String groupName = userGroup.getGroup_name();
        String reachCount = userGroup.getReach_count();
        String rules = userGroup.getRules();
        String isOnline = userGroup.getIs_online();

        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        String createTime = formatter.format(date);
        String creater = "管理员";
        //  获取当前的groupId
        String groupIdNow = userGroupMapper.searchUserGroupId();
        Integer groupId = Integer.parseInt(groupIdNow) + 2;

        userGroupMapper.addUsergroup(String.valueOf(groupId), groupName, creater, createTime, reachCount, isOnline, rules);
        return CommonUtil.successJson();
    }


    /**
     *  删除用户人群
     */
    public JSONObject deleteUsergroup(String group_id){
        userGroupMapper.deleteUsergroup(group_id);
        return CommonUtil.successJson();
    }


    /**
     *  修改用户人群
     */
    public JSONObject updateUsergroup(UserGroupChange userGroupChange){
        String groupId = userGroupChange.getGroup_id();
        String groupName = userGroupChange.getGroup_name();
        String reachCount = userGroupChange.getReach_count();
        String rules = userGroupChange.getRules();
        String isOnline = userGroupChange.getIs_online();

        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        String createTime = formatter.format(date);
        String creater = "管理员";
        userGroupMapper.updateUsergroup(groupId, groupName, creater, createTime, reachCount, isOnline, rules);
        return CommonUtil.successJson();
    }


    /**
     *  根据人群名称进行查询
     */
    public JSONObject searchUsergroup(String groupName){
        List<JSONObject> list = userGroupMapper.searchUsergroup(groupName);
        JSONObject returnResult = new JSONObject(true);
        returnResult.put("list", list);
        return returnResult;
    }


    /**
     *  根据人群ID进行查询
     */
    public JSONObject searchUsergroupById(String groupId){
        List<JSONObject> list = userGroupMapper.searchUsergroupById(groupId);
        JSONObject returnResult = new JSONObject(true);
        returnResult.put("list", list);
        return returnResult;
    }


    /**
     *  选择人群分析维度
     */
    public List<String> analysisDimension(){
        List<String> list = userGroupMapper.analysisDimension();
        return list;
    }


    /**
     *  选择人群分析维度-备用 分布分析模块用
     */
    public List<String> dimensionDistribute(){
        List<String> list = userGroupMapper.dimensionDistribute();
        return list;
    }


}
